<?php
/**
 * Created by PhpStorm.
 * User: xiao
 * Date: 2017/11/30
 * Time: 10:39
 */

namespace Home\Controller;


use Common\Api\WxApi;
use Common\Base\BaseController;
use Org\WeChat\WeChat;
use Org\WeChat\WeChatApi;
use Org\WeChat\WeChatUtils;
use Org\WeChat\WeChatZd;

class WeChatAutoController extends BaseController
{
    public function ordertests()
    {
        $res = M('user')->find();

        $this->assign('result',$res);

        $this->display("Index:ordertest");
    }

    /**
     * 测试
     */
    public function orderSendtest()
    {
        $where['id'] = I('post.userid');

        $opid = M('user')->where($where)->find();

        $openid = $opid['openid'];  // oGZlvw3Nvl4FTrUjgiwxowXKe_HU     //用户的openid

        $template = "km-h1nl05HZdXOeDPNJX5e4JdeRlg1TndTo5Yn5gqhc";      //模板id

        $Jump = "http://www.baidu.com";                                 //跳转路径

        $goods_name = "测试商品";                                       //商品标题

        $shop_price = "66元";                                          //金额

        $state = "成功";                                               //状态

        $res = $this->ordersSend( $openid , $Jump , $template ,  $goods_name , $shop_price , $state );

        var_dump(json_decode($res));
    }

    /**
     * 微信模版行业设置
     */
    public function ordertest()
    {
        $weChat = new WeChat();

        $accessToken = $weChat->GetAccessToken();               //获取accessToken

        $url = "https://api.weixin.qq.com/cgi-bin/template/api_set_industry?access_token=" . $accessToken;  //url

        $arr = array(                                           //post 数据包

            "industry_id1"=>"1",                                // 主行业

            "industry_id2"=>"4"                                 // 次行业  参考微信公众号文档

        );
        $json_arr = json_encode($arr);                          //转化为json类型数据

        $WeChatZd = new WeChatZd;

//        $result = $WeChatZd->getTemplate($accessToken);

        $res = $WeChatZd->request_by_curl($url,$json_arr);      //微信模版设置请求

        dump($res);
    }

    /**
     * @return mixed
     * 获取模板列表
     */
    public function orderget()
    {
        header("Content-type: text/html; charset=utf-8");

        $weChat = new WeChat();

        $WeChatZd = new WeChatZd;

        $accessToken = $weChat->GetAccessToken();               //获取accessToken

        $url = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=" . $accessToken;  //url

        $res = $WeChatZd->request_by_curl($url);               //获取模版列表

//        dump($res);

        return $res;                                            //返回列表信息
    }

    /**
     * 获得模板ID
     */
    public function ordernews()
    {
        $weChat = new WeChat();

        $WeChatZd = new WeChatZd;

        $accessToken = $weChat->GetAccessToken();                //获取accessToken

        $url = "https://api.weixin.qq.com/cgi-bin/template/api_add_template?access_token=" . $accessToken;

        $arr = array(                                           //数据包

            "template_id_short"=>"TM00015"

        );

        $json_arr = json_encode($arr);                          //转化为json类型数据

        $result = $WeChatZd->request_by_curl($url , $json_arr);

        dump(json_decode($result));
    }

    /**
     * @param $openid       @用户openid
     * @param $Jump         @跳转路径
     * @param $template     @模板id
     * @param $goods_name   @商品名字
     * @param $shop_price   @商品价格
     * @param $state        @状态 （成功 或 失败）
     * @return mixed
     * 发送模版信息
     */
    public function ordersSend($openid , $Jump , $template ,  $goods_name , $shop_price , $state )
    {
        $arr = array(                                                           //根据模版设置

            "touser" => $openid,                                                //用户openid

//            "template_id"=>"km-h1nl05HZdXOeDPNJX5e4JdeRlg1TndTo5Yn5gqhc",      //模版id

            "template_id"=>$template,                                           //模版id

            "url" => $Jump,                                                     //跳转路径

            "data" => array(                                                    //POST 数据包 修改的数据内容
                "key1" => array(
                    "value" => $goods_name,
                    "color" => "#173177",
                ),
                "key2" => array(
                    "value" => $shop_price,
                    "color" => "#173177",
                ),
                "key3" => array(
                    "value" => $state,
                    "color" => "#173177",
                )
            )

        );
        return $this->orderSendNews($arr);
    }

    /**
     * @param $data         @ POST数据包
     * @return mixed
     * 发送模板信息
     */
    public function orderSendNews( $data )
    {
        $weChat = new WeChat();

        $WeChatZd= new WeChatZd;

        $accessToken = $weChat->GetAccessToken();                           //获取accesstoken

        $url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=" . $accessToken;

        $arr = $data;                                                       //POST数据包

        $json_arr =  json_encode($arr);

        $res = $WeChatZd->request_by_curl($url,$json_arr);                 //发送微信模板

        return $res;

    }

}
